package com.ficus.road.maintain.third.zh.bridgehotel;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotel;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotelZH;
import com.ficus.road.maintain.dao.BridgeHotelDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/22 下午2:06
 */
@Component
@Slf4j
public class ZHBridgeHotelInfo extends BaseZH {
    @Value("${zhonghai.syncBridgeHotel}")
    private Boolean syncBridgeHotel;
    @Autowired
    private BridgeHotelDao bridgeHotelDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetBridgeHotelData";
        arg1 = "{}";
        errMsg = "调用中海接口获取桥下空间基础数据异常!";
        successMsg = "调用中海接口获取桥下空间基础数据成功!";
    }

    //        @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?", zone = "GMT+8:00")
    public void refreshBridgeHotelBasicInfo() {
        Date today = new Date();
        List<BridgeHotel> bridgeHotelList = bridgeHotelDao.findAll();
        Map<String, BridgeHotel> bridgeHotelMap = bridgeHotelList.stream().collect(Collectors.toMap(BridgeHotel::getDeviceId, Function.identity()));
        String bridgeHotelStr = null;
        int i = 0;
        while (i < 10) {
            bridgeHotelStr = getZHData();
            if (ObjectUtils.isNotEmpty(bridgeHotelStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(bridgeHotelStr)) {
            log.warn("{}: 未同步到中海桥下空间告警信息！", MyDateUtil.dateFormatStr(today, "yyyy-MM-dd"));
            return;
        }
        BridgeHotel bridgeHotel;
        List<BridgeHotelZH> bridgeHotelZHList = JSONArray.parseArray(bridgeHotelStr, BridgeHotelZH.class);
        for (BridgeHotelZH zh : bridgeHotelZHList) {
            if (bridgeHotelMap.containsKey(zh.getDeviceid())) {
                // 增量修改，防止pictureAddress消失
                bridgeHotel = bridgeHotelMap.get(zh.getDeviceid());
                bridgeHotel.setMonitorType(zh.getMonitortype());
                bridgeHotel.setAddress(zh.getAddress());
                bridgeHotel.setCreateDate(MyDateUtil.strParseDate(zh.getCreatedate(), "yyyy-MM-dd'T'HH:mm:ss"));
                bridgeHotel.setBridgeName(zh.getBridgename());
                bridgeHotel.setRoadId(zh.getRoadid());
                bridgeHotel.setRoadName(zh.getRoadname());
                bridgeHotel.setModificationTime(today);
            } else {
                bridgeHotel = zh.convert2BridgeHotel(zh, today, MyDateUtil.strParseDate(zh.getCreatedate(), "yyyy-MM-dd'T'HH:mm:ss"));
            }
            bridgeHotelDao.saveOrUpdate(bridgeHotel);
        }
    }
}
